package common.classes;

import java.io.*;

public final class FileNameWorker {

private static boolean dirContainsFile(final String fullFilePath) {
	File file = new File(fullFilePath);
	if (file.exists()) {
		return true;
	}
	return false;
}

public static String createFullFilePath(final String path, final String fileName) {
	if (fileName.contains("/../"))  {
		return "";
	}
	String buff = new String(fileName);
	if (path.endsWith("/") && buff.startsWith("/")) {
		buff = buff.substring(1, buff.length());
	}
	buff = path + buff;
	if (buff.endsWith("/")) {
		buff += "index.html";
	}
	if (!dirContainsFile(buff)) 
		return "";
	if (!serverSupportFileType(buff)) 
		return "";
	return buff;
}

private static boolean serverSupportFileType(String fullFilePath){
	if (fullFilePath.endsWith("html")) return true;
	if (fullFilePath.endsWith("htm")) return true;
	if (fullFilePath.endsWith("jpeg")) return true;
	if (fullFilePath.endsWith("jpg")) return true;
	if (fullFilePath.endsWith("gif")) return true;
	if (fullFilePath.endsWith("css")) return true;
	if (fullFilePath.endsWith("js")) return true;
	return false;
}

public static String contentTypeOf(String fullFilePath) {
	if (fullFilePath.endsWith("jpeg")) return "image/jpeg";
	if (fullFilePath.endsWith("jpg")) return "image/jpeg";
	if (fullFilePath.endsWith("gif")) return "image/gif";
	if (fullFilePath.endsWith("css")) return "text/css";
	if (fullFilePath.endsWith("js")) return "application/x-javascript";
	return "text/html";
}

}
